﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Skin
{
    public class SkinInfo
    {
        public string Name { get; set; }
        public string Version { get; set; }
        public string Author { get; set; }
        public string Path { get; set; }
    }

    public static class SkinManager
    {
        const string name = "Name";
        const string version = "Version";
        const string author = "Author";
        const string _path = "Path";
        const string grbrush = "GradientBrush";
        const string whats = "What";
        const string type = "Type";
        const string source = "Source";
        const string lgrbrush = "LinearGradientBrush";
        const string rgrbrush = "RadialGradientBrush";
        const string sgrbrush = "SolidColorBrush";
        const string imgbrush = "ImageBrush";
        const string _stops = "GradientStops";
        const string _stop = "GradientStop";
        const string _offset = "Offset";
        const string _color = "Color";

       public static XDocument doc;
       public static string path;
       public static string folder;

        public static SkinInfo GetSkinInfo()
        {
            SkinInfo info = new SkinInfo();
            info.Name = doc.Root.Attribute(name).Value;
            info.Version = doc.Root.Attribute(version).Value;
            info.Author = doc.Root.Attribute(author).Value;
            info.Path = doc.Root.Attribute(_path).Value;
            return info;
        }

        public static SkinInfo GetSkinInfo(string file)
        {
            XDocument local = XDocument.Load(file);
            SkinInfo info = new SkinInfo();
            info.Name = local.Root.Attribute(name).Value;
            info.Version = local.Root.Attribute(version).Value;
            info.Author = local.Root.Attribute(author).Value;
            info.Path = local.Root.Attribute(path).Value;
            return info;
        }

        public static Brush GetBrush(string control, string what)
        {
            try
            {
                if (doc != null && doc.Root != null)
                {
                    XElement element = doc.Root.Element(control);
                    XElement brush;
                    brush = element.Elements(grbrush).Where(t => t.Attribute(whats).Value == what).First();
                    Brush color = Brushes.White;
                    switch (brush.Attribute(type).Value)
                    {
                        case lgrbrush:
                            {
                                color = new LinearGradientBrush();
                                List<XElement> stops = brush.Element(_stops).Elements(_stop).ToList();
                                (color as LinearGradientBrush).GradientStops = new GradientStopCollection();
                                foreach (XElement stop in stops)
                                {
                                    double offset = Convert.ToDouble(stop.Attribute(_offset).Value);
                                    string[] bytes = stop.Attribute(_color).Value.Split(',');
                                    Color bc = Colors.Transparent;
                                    if (bytes.Length == 4)
                                        bc = new Color() { A = Convert.ToByte(bytes[0]), R = Convert.ToByte(bytes[1]), G = Convert.ToByte(bytes[2]), B = Convert.ToByte(bytes[3]) };
                                    else
                                        bc = new Color() { A = 255, R = Convert.ToByte(bytes[0]), G = Convert.ToByte(bytes[1]), B = Convert.ToByte(bytes[2]) };
                                    GradientStop gs = new GradientStop(bc, offset);
                                    (color as LinearGradientBrush).GradientStops.Add(gs);
                                }
                                (color as LinearGradientBrush).Freeze();
                                break;
                            }
                        case rgrbrush:
                            {
                                color = new RadialGradientBrush();
                                List<XElement> stops = brush.Element(_stops).Elements(_stop).ToList();
                                (color as RadialGradientBrush).GradientStops = new GradientStopCollection();
                                foreach (XElement stop in stops)
                                {
                                    double offset = Convert.ToDouble(stop.Attribute(_offset).Value);
                                    string[] bytes = stop.Attribute(_color).Value.Split(',');
                                    Color bc = Colors.Transparent;
                                    if (bytes.Length == 4)
                                        bc = new Color() { A = Convert.ToByte(bytes[0]), R = Convert.ToByte(bytes[1]), G = Convert.ToByte(bytes[2]), B = Convert.ToByte(bytes[3]) };
                                    else
                                        bc = new Color() { A = 255, R = Convert.ToByte(bytes[0]), G = Convert.ToByte(bytes[1]), B = Convert.ToByte(bytes[2]) };                                    GradientStop gs = new GradientStop(bc, offset);
                                    (color as RadialGradientBrush).GradientStops.Add(gs);
                                }
                                (color as RadialGradientBrush).Freeze();
                                break;
                            }
                        case sgrbrush:
                            {
                                color = new SolidColorBrush();
                                string[] bytes = brush.Value.Split(',');
                                Color bc = new Color() { R = Convert.ToByte(bytes[0]), G = Convert.ToByte(bytes[1]), B = Convert.ToByte(bytes[2]), A = 255 };
                                (color as SolidColorBrush).Color = bc;
                                break;
                            }
                        case imgbrush:
                            {
                                string str = brush.Attribute(source).Value;
                                string src = folder + str;
                                color = new ImageBrush(BitmapFrame.Create(new Uri(src)));
                                break;
                            }
                    }
                    return color;
                }
                else
                {
                    return Brushes.Transparent;
                }
            }
            catch { return Brushes.Transparent; }
        }
        
    }
}
